package com.example.logistics_oa.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.comment_oa.pojos.FlApplycong;
import com.example.comment_oa.pojos.FlApplyzhu;
import com.example.comment_oa.pojos.SysEmployee;
import com.example.comment_oa.response.AjaxResponse;
import com.example.logistics_oa.controller.util.SendEmail;
import com.example.logistics_oa.service.FlApplycongService;
import com.example.logistics_oa.service.FlApplyzhuService;
import org.apache.commons.mail.EmailException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @Description:
 * @Author:李博文
 * @Date:2022/10/27 15:39
 *
 * 福利从表的操作
 */
@RestController
@RequestMapping("/logisticsOa")
@CrossOrigin
public class WelfareController {
    @Autowired
    FlApplyzhuService flApplyzhuService;
    @Autowired
    FlApplycongService flApplycongService;

    @PostMapping("/zengzidaun1")
    public AjaxResponse xiznengzidaun(@RequestBody FlApplyzhu flApplyzhu){
        int i = flApplyzhuService.insertZiDuan(flApplyzhu);
        return AjaxResponse.success();
    }

    @PostMapping("/zengCongBiao")
    public AjaxResponse zengfulicongbiao(@RequestBody FlApplycong flApplycong){
        List<FlApplycong> flApplycongs = flApplycongService.DanGeQuChong(flApplycong.getFlcBianhao(), flApplycong.getFlcGonghao());
        if(flApplycongs.isEmpty()){
            int i = flApplycongService.insertCong(flApplycong);
            return AjaxResponse.success();
        }else {
            return AjaxResponse.dosentexit();
        }

    }

    @GetMapping("/genjutiaojianchacongbiao/{flcBianhao}")
    public AjaxResponse genjutiaojianchacongbiao(@PathVariable("flcBianhao")String flcBianhao){
        List<FlApplycong> flApplycongs = flApplycongService.GenJuDanHaoChaXun(flcBianhao);
        return AjaxResponse.success(flApplycongs);
    }

    @PostMapping("/chuanjihe/{flcBianhao}")
    public AjaxResponse chuanJiHe(@PathVariable("flcBianhao")String flcBianhao,@RequestParam("json")String json){
        //前台传过来的编号
        System.err.println(flcBianhao);

        //这个单号下面已经存在的候选人
        List<FlApplycong> flApplycongs = flApplycongService.GenJuDanHaoChaXun(flcBianhao);

        //要新增进去的集合
        List<SysEmployee> sysEmployee = JSONArray.parseArray(json,SysEmployee.class);


        //通过for循环 判断去重FlApplycong flApplycong : flApplycongs
//        for (SysEmployee employee : sysEmployee) {
//            for (FlApplycong flApplycong : flApplycongs) {
//                if(flApplycong.getFlcGonghao().equals(employee.getEmpCard())){
//                    continue;
//                }else{
//                    FlApplycong flApplycong1 = new FlApplycong();
//                    flApplycong1.setFlcBianhao(flcBianhao);
//                    flApplycong1.setFlcDepartmentname(employee.getDept().getDeptName());
//                    flApplycong1.setFlcJobname(employee.getEmployeePost().getPost().getPostName());
//                    flApplycong1.setFlcName(employee.getEmpName());
//                    flApplycong1.setFlcGonghao(employee.getEmpCard());
//                    flApplycong1.setFlcSex(employee.getEmpSex());
//                    flApplycong1.setFlcHunlian(employee.getEmpMarriage());
//                    flApplycong1.setFlcZhengzhi(employee.getEmpPolitical());
//                    flApplycong1.setFlcEmpstatus(employee.getEmpState());
//                    flApplycong1.setFlcYopuxaing(employee.getEmpEmail());
//                    flApplycong1.setFlcBankcard(employee.getEmpBank());
//                    flApplycongService.insertCong(flApplycong1);
//                }
//            }
//        }flApplycongs


        for (int i = 0; i < flApplycongs.size(); i++) {
            for (int j = 0; j < sysEmployee.size(); j++) {
                System.err.println("s"+sysEmployee.get(j).getEmpCard());
                System.err.println("y"+flApplycongs.get(i).getFlcGonghao());
                if(sysEmployee.get(j).getEmpCard().equals(flApplycongs.get(i).getFlcGonghao())){
                    sysEmployee.remove(j) ;
                    j = j-1;
                }else{


                }
            }
        }
        int  zengjia =0;
        for (int j = 0; j < sysEmployee.size(); j++) {
            FlApplycong flApplycong1 = new FlApplycong();
            flApplycong1.setFlcBianhao(flcBianhao);
            flApplycong1.setFlcDepartmentname(sysEmployee.get(j).getDept().getDeptName());
            flApplycong1.setFlcJobname(sysEmployee.get(j).getEmployeePost().getPost().getPostName());
            flApplycong1.setFlcName(sysEmployee.get(j).getEmpName());
            flApplycong1.setFlcGonghao(sysEmployee.get(j).getEmpCard());
            flApplycong1.setFlcSex(sysEmployee.get(j).getEmpSex());
            flApplycong1.setFlcHunlian(sysEmployee.get(j).getEmpMarriage());
            flApplycong1.setFlcZhengzhi(sysEmployee.get(j).getEmpPolitical());
            flApplycong1.setFlcEmpstatus(sysEmployee.get(j).getEmpState());
            flApplycong1.setFlcYopuxaing(sysEmployee.get(j).getEmpEmail());
            flApplycong1.setFlcBankcard(sysEmployee.get(j).getEmpBank());
            zengjia = flApplycongService.insertCong(flApplycong1);
        }
        if(zengjia>0){
            return AjaxResponse.success();
        }else {
            return AjaxResponse.dosentexit();

        }
    }

    @DeleteMapping("/DeleteById/{flcId}")
    public AjaxResponse genjuidshanchu(@PathVariable("flcId")int flcId){
        flApplycongService.DeleteById(flcId);
        return  AjaxResponse.success();
    }

    @DeleteMapping("/DeleteByBianHao/{flcBianhao}")
    public AjaxResponse genjuBianHaoshanchu(@PathVariable("flcBianhao")String flcBianhao){
        //System.err.println("???"+flcBianhao);
        flApplycongService.DeleteByBianHao(flcBianhao);
        return  AjaxResponse.success();
    }

    @PostMapping("/xiugaiZiDuan2")
    public AjaxResponse xiugaiziDuan2(@RequestBody FlApplyzhu flApplyzhu){
        flApplyzhuService.updaZiDuan2(flApplyzhu);
        return AjaxResponse.success();
    }

    @PostMapping("/selectall/zhubiao")
    public AjaxResponse selectmei0(@RequestBody FlApplyzhu flApplyzhu){
        System.err.println(flApplyzhu);
        List<FlApplyzhu> flApplyzhus = flApplyzhuService.selectMeiyou0(flApplyzhu);
        return AjaxResponse.success(flApplyzhus);
    }

    @DeleteMapping("/FanHuiBuBaoCun/shuangbiao/{flcBianhao}")
    public AjaxResponse fanhuibubaocun(@PathVariable("flcBianhao")String flcBianhao){

        flApplycongService.DeleteByBianHao(flcBianhao);
        flApplyzhuService.DeleteZhu(flcBianhao);
        return AjaxResponse.success();
    }


    @GetMapping("/ZhuAndCong/{flBianhao}")
    public AjaxResponse zhubiaohecongbiao(@PathVariable("flBianhao")String flBianhao){
        System.err.println(flBianhao);
        FlApplyzhu flApplyzhu = flApplyzhuService.selectZhuAndCong(flBianhao);
        List<FlApplycong> flApplycongs = flApplycongService.selectAllByFlcBianhao(flBianhao);
        flApplyzhu.setFlApplycongs(flApplycongs);

        return AjaxResponse.success(flApplyzhu);
    }

    @GetMapping("/FuLiYouJian/{shoujianren}")
    public AjaxResponse fayouyoujian(@PathVariable("shoujianren") String shoujianren) throws EmailException {
        SendEmail sendEmail = new SendEmail();
        System.out.println(shoujianren);
        sendEmail.sendSimpleTextEmail(shoujianren);
        return  AjaxResponse.success();
    }

    @GetMapping("/xiugaicongbiao/{flcId}")
    public AjaxResponse xiugaiccongbiao(@PathVariable("flcId") int flcId) throws EmailException {
        int i = flApplycongService.updateByID(flcId);
        return  AjaxResponse.success();
    }

    @GetMapping("/xiugaicongbiao2/{bianhao}")
    public AjaxResponse xiugaiccongbiao2(@PathVariable("bianhao") String bianhao) throws EmailException {
        int i = flApplycongService.updateByFlBianHao(bianhao);
        return  AjaxResponse.success();
    }




}
